package com.sangfor.ssl.service.netmonitor;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import java.util.Locale;

/* loaded from: classes2.dex */
public class NetworkHelper {
    protected static final String DEFAULT_ADDR = "0.0.0.0";
    private static final String TAG = "NetworkInfoHelper";
    public static final int TYPE_MOBILE = 0;
    public static final int TYPE_NONE = -1;
    public static final int TYPE_WIFI = 1;

    public static int getNetworkType(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {
            if (activeNetworkInfo.getType() == 1) {
                return 1;
            }
            if (activeNetworkInfo.getType() == 0) {
                return 0;
            }
        }
        return -1;
    }

    private static NetworkInfo getActiveNetworkInfo(Context context) {
        return ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
    }

    public static boolean isNetworkConnected(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public static boolean isWiFiConnected(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService("connectivity");
        return connectivityManager != null && connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().getType() == 1 && connectivityManager.getActiveNetworkInfo().isConnected();
    }

    public static String getWiFiSSID(Context context) {
        WifiInfo connectionInfo = ((WifiManager) context.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
        return connectionInfo != null ? connectionInfo.getSSID() : "";
    }

    public static String getWiFiIPAddress(Context context) {
        WifiInfo connectionInfo = ((WifiManager) context.getApplicationContext().getSystemService("wifi")).getConnectionInfo();
        if (connectionInfo != null) {
            return intToIp(connectionInfo.getIpAddress());
        }
        return null;
    }

    public static String getGatewayIP(Context context) {
        DhcpInfo dhcpInfo = ((WifiManager) context.getApplicationContext().getSystemService("wifi")).getDhcpInfo();
        return dhcpInfo != null ? intToIp(dhcpInfo.gateway) : "";
    }

    private static String intToIp(int i) {
        return String.format(Locale.getDefault(), "%d.%d.%d.%d", Integer.valueOf(i & 255), Integer.valueOf((i >> 8) & 255), Integer.valueOf((i >> 16) & 255), Integer.valueOf((i >> 24) & 255));
    }

    public static String getNetworkOperatorName(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
        return telephonyManager != null ? telephonyManager.getNetworkOperatorName() : "";
    }

    public static String getNetworkOperator(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
        return telephonyManager != null ? telephonyManager.getNetworkOperator() : "";
    }

    public static String getWiFiCheckInfo(Context context) {
        String wiFiSSID = getWiFiSSID(context);
        String gatewayIP = getGatewayIP(context);
        Log.debug(TAG, "ssid:" + wiFiSSID + ", gateway:" + gatewayIP);
        return (TextUtils.isEmpty(wiFiSSID) || TextUtils.isEmpty(gatewayIP) || TextUtils.equals(wiFiSSID, DEFAULT_ADDR) || TextUtils.equals(gatewayIP, DEFAULT_ADDR)) ? "" : String.format(Locale.getDefault(), "%s@%s", wiFiSSID, gatewayIP);
    }

    public static String getMobileCheckInfo(Context context) {
        return getNetworkOperator(context);
    }

    public static String getNetworkCheckInfo(Context context, int i) {
        if (i == 0) {
            return getMobileCheckInfo(context);
        }
        return 1 == i ? getWiFiCheckInfo(context) : "";
    }

    public static int getSubType(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        if (activeNetworkInfo == null || !activeNetworkInfo.isAvailable()) {
            return -1;
        }
        return activeNetworkInfo.getSubtype();
    }

    public static String getSubtypeName(Context context) {
        NetworkInfo activeNetworkInfo = getActiveNetworkInfo(context);
        return (activeNetworkInfo == null || !activeNetworkInfo.isAvailable()) ? "" : activeNetworkInfo.getSubtypeName();
    }
}
